﻿/**
 * Main movie clip settings.
 * This script is included by the spreadshop.fla file.
 */

// Some global stage settings.
_quality = "BEST";
Stage.align = "TL";
Stage.scaleMode = "noScale";
Stage.showMenu = false;

/**
 * Creates a loader bar and a textfield
 * and shows the loading in progress.
 */
var loader:MovieClip = createEmptyMovieClip("mc_loader", 0);
loader.init = function():Void {
	// Creates an alias to the root movie clip.
	this.main = this._parent;
	// Stay on first frame while loading.
	this.main.stop();
	
	// Creates a loader bar to show loading in progress.
	this.bar = this.createEmptyMovieClip("mc_bar", 0);
	with(this.bar) {
		beginFill(0x0099ff, 100);
		lineTo(Stage.width, 0);
		lineTo(Stage.width, 20);
		lineTo(0, 20);
		endFill();
	}
	// Create a text field to show the percentage.
	this.tfp = this.createTextField("tf_percentage", 1, 2, 0, 200, 20);
	this.tfp.selectable = false;
	this.tfp.textColor = 0xffffff;
	this.tff = new TextFormat();
	this.tff.font = "Verdana";
	this.tff.size = 12;
	this.tff.bold = true;
	this.tff.letterSpacing = -1;
	// Scales the loader bar and shows the percentage.
	this.setValues = function(p:Number):Void {
		this.bar._xscale = p;
		this.tfp.text = p +" %";
		this.tfp.setTextFormat(this.tff);
	};
	// Setup values.
	this.pi = 0;
	this.delay = 10;
	this.setValues(this.pi);
	// Start loader.
	this.onEnterFrame = this.onPreload;
	
};

/**
 * Show loading in progress.
 * This method is called every frame, staying on the first.
 */
loader.onPreload = function():Void {
	// Get status.
	var bl:Number = this.main.getBytesLoaded();
	var bt:Number = this.main.getBytesTotal();
	var p:Number = (bl > 0) ? bl / bt * 100 >> 0 : 0;
	// Increase virtual percentage.
	var i:Number = 7;
	while((i-- > 0) && (this.pi < p)) {
		this.pi++;
	}
	// Set Values
	this.setValues(this.pi);
	// Check if loading is complete.
	if(this.pi >= 100) {
		this.setValues(100);
		this.onEnterFrame = this.onDone;
	}
};

/**
 * Initializes the main application an removes the loader clip.
 */
loader.onDone = function():Void {
	if(this.delay-- < 0) {
		this.tfp.text = "";
		this.bar._x += (Stage.width - this.bar._x) * .25;
		if(this.bar._x > Stage.width - 2) {
			delete this.onEnterFrame;
			this.main.gotoAndStop("init");
			this.removeMovieClip();
		}
	}
}

// Starts preloading the main movie clip.
loader.init();
